package com.hilin.etcd;

import com.hilin.etcd.client.HilinEtcdClient;
import com.hilin.etcd.config.HilinEtcdConfig;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.util.ObjectUtils;

import java.net.URI;

@EnableConfigurationProperties({
        HilinEtcdConfig.class
})
public class HilinEtcdAutoConfig {

    private final HilinEtcdConfig etcdConfig;

    public HilinEtcdAutoConfig(HilinEtcdConfig etcdConfig) {
        this.etcdConfig = etcdConfig;
    }

    @Bean
    public HilinEtcdClient etcdClient() {
        if (ObjectUtils.isEmpty(etcdConfig.getUri())) {
            return new HilinEtcdClient();
        } else {
            URI[] uris = new URI[etcdConfig.getUri().length];
            for (int i = 0; i < etcdConfig.getUri().length; i++) {
                uris[i] = URI.create(etcdConfig.getUri()[i]);
            }
            return new HilinEtcdClient(uris);
        }
    }

}
